package cn.djb.day27;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 给定一个数组 nums 和滑动窗口的大小 k，请找出所有滑动窗口里的最大值
 */
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length == 0) return new int[0];

        int[] res = new int[nums.length - k + 1];

        PriorityQueue<Integer> sortQueue = new PriorityQueue<>(Comparator.reverseOrder());

        for (int i = 0; i < k; i++) {
            sortQueue.offer(nums[i]);
        }
        res[0] = sortQueue.peek();
        int left = nums[0];
        for (int i = k; i < nums.length; i++) {
            sortQueue.remove(left);
            sortQueue.offer(nums[i]);
            left = nums[i];
            res[i - k + 1] = sortQueue.peek();
        }
        return res;


    }
}